package com.xxtx.trade.common.utils;



import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class SendSMSUtil {
    public static final String accountSid="1a2880d6ff56a85740ae406e59bc9980";//秘钥
    //   public static final String templateid="171982";//短信模板id
    public static final String templateid="435072";//短信模板id
    public static final String smsType="100000";//消息类型为：验证码类型 100003 营销
    public static final String token="00da89882ee87b50132b20fdfe0f6d0e";//token

    /**
     * post请求
     *
     * @param phone
     *            手机号
     * @param param
     *            要发送的验证码
     * @return
     */
    public static boolean sendSMS(String phone, String param) {
        boolean result=false;
        try {
            String body = getBody(phone, param);
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL("https://openapi.danmi.com/distributor/sendSMS");
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            StringBuilder builder = new StringBuilder();
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    builder.append(System.lineSeparator());
                }
                builder.append(line);
            }
            String string = JSON.parseObject(builder.toString()).getString("respCode");
            if (string.equals("0000")){
                result=true;
            }
        } catch (Exception e) {
            result=false;
            e.printStackTrace();
        }
        return result;
    }
    /**
     * post请求
     *
     * @param phone
     *            手机号
     * @param param
     *            要发送的验证码
     * @return
     */
    public static boolean sendSMS(String phone, String param,String smsId) {
        boolean result=false;
        try {
            String body = getBody(phone, param,smsId);
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL("https://openapi.danmi.com/distributor/sendSMS");
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            StringBuilder builder = new StringBuilder();
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    builder.append(System.lineSeparator());
                }
                builder.append(line);
            }
            String string = JSON.parseObject(builder.toString()).getString("respCode");
            if (string.equals("0000")){
                result=true;
            }
        } catch (Exception e) {
            result=false;
            e.printStackTrace();
        }
        return result;
    }
    public static String getBody(String phone,String param,String smsID){
        long timestamp = System.currentTimeMillis();
        String sig = MD5.MD5Encode(accountSid + token + timestamp);
        String body="accountSid="+accountSid+"&to="+phone+"&templateid="+smsID+"&param="+param+"&timestamp="+timestamp+"&smsType="+"100000"+"&sig="+sig;
        return body;
    }
    public static String getBody(String phone,String param){
        long timestamp = System.currentTimeMillis();
        String sig = MD5.MD5Encode(accountSid + token + timestamp);
        String body="accountSid="+accountSid+"&to="+phone+"&templateid="+templateid+"&param="+param+"&timestamp="+timestamp+"&smsType="+smsType+"&sig="+sig;
        return body;
    }

    public static void main(String[] args) {
        SendSMSUtil.sendSMSVoice("13760447353", "测试,85","435074");

    }

    public static Boolean sendSMSVoice(String phone, String param, String smsId) {
        boolean result=false;
        try {
            String body = getBody(phone, param,smsId);
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL("https://openapi.danmi.com/distributor/sendSMS");
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            StringBuilder builder = new StringBuilder();
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    builder.append(System.lineSeparator());
                }
                builder.append(line);
            }
            String string = JSON.parseObject(builder.toString()).getString("respCode");
            if (string.equals("0000")){
                result=true;
            }
        } catch (Exception e) {
            result=false;
            e.printStackTrace();
        }
        return result;
    }
}
